package com.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Customer;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.CustomerPageVo;
import com.insurance.domain.vo.PageVo;
import com.insurance.mapper.CustomerMapper;
import com.insurance.service.CustomerService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Customer)表服务实现类
 *
 * @author makejava
 * @since 2023-04-22 09:43:50
 */
@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Override
    public ResponseResult addCustomer(Customer customer) {
        if(save(customer)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.ADD_FAILED);
    }

    @Override
    public ResponseResult updateCustomer(Customer customer) {
        if(updateById(customer)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult listByPage(CustomerPageVo customerPageVo) {
        LambdaQueryWrapper<Customer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(customerPageVo.getName()),Customer::getName,customerPageVo.getName());
        queryWrapper.eq(!ObjectUtils.isEmpty(customerPageVo.getCountry()),Customer::getCountry,customerPageVo.getCountry());
        queryWrapper.like(!ObjectUtils.isEmpty(customerPageVo.getBirthPlace()),Customer::getBirthPlace,customerPageVo.getBirthPlace());
        queryWrapper.like(!ObjectUtils.isEmpty(customerPageVo.getPhoneNumber()), Customer::getPhoneNumber,customerPageVo.getPhoneNumber());
        Page<Customer> page = new Page<>(customerPageVo.getPageNo(), customerPageVo.getPageSize());
        page(page,queryWrapper);
        List<Customer> customers = page.getRecords();
        PageVo pageVo = new PageVo(customers, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
}
